package com.media.config;

import com.media.pojo.CameraPojo;
import com.media.pojo.ResourceModel;
import com.media.service.listener.HandlerResourceListener;
import com.media.service.listener.RecordResourceListener;
import com.media.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;

@Configuration
@Slf4j
@Profile({"prod"})
public class ScheduleTaskConfig {

    /**
     * rtsp 转视频
     */
    @Resource(name = "rtspVideoRecordService")
    RecordResourceListener rtspVideoRecordService;


    /**
     * rtsp 转图片
     */
    @Resource(name = "rtspImageRecordService")
    RecordResourceListener rtspImageRecordService;

    /**
     * 处理图片
     */
    @Resource(name = "imageHandlerService")
    HandlerResourceListener imageHandlerService;


    /**
     * 项目启动时立即执行一次(开始录制视频 (图片十分钟后会自动录制))
     */
    @EventListener(ApplicationReadyEvent.class)
    public void onApplicationEvent() {
//      1 : 录制视频
        recordVideo();
//      2 : 录制图片
        recordImage();
    }

    /**
     * 视频录制每个小时整点执行 (一个视频一个小时)
     */
    @Scheduled(cron = "0 0 * * * ?")
    public void recordVideo() {
        log.info("小时 : {}", DateUtils.getTime());
        ResourceModel resourceModel = new ResourceModel();
        CameraPojo cameraPojo = new CameraPojo();
        cameraPojo.setId(1L);
        cameraPojo.setIp("192.168.8.205");
        cameraPojo.setUsername("admin");
        cameraPojo.setPassword("xhm123456");
        cameraPojo.setChannel("34020000001320000001");
        cameraPojo.setStream("main");
        resourceModel.setCameraPojo(cameraPojo);
        rtspVideoRecordService.record(resourceModel, null);
    }

    /**
     * 每十分钟整点执行一次
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void recordImage() {
        log.info("分钟 : {}", DateUtils.getTime());
        ResourceModel resourceModel = new ResourceModel();
        CameraPojo cameraPojo = new CameraPojo();
        cameraPojo.setId(1L);
        cameraPojo.setIp("192.168.8.205");
        cameraPojo.setUsername("admin");
        cameraPojo.setPassword("xhm123456");
        cameraPojo.setChannel("34020000001320000001");
        cameraPojo.setStream("main");
        resourceModel.setCameraPojo(cameraPojo);
        rtspImageRecordService.record(resourceModel, imageHandlerService);
    }

}
